﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKDimensionModel.DetailIndex.Jig;

namespace YJKDimensionModel.DetailIndex.Operate
{
	internal class NoRangeDetailIndexOperate : DetailIndexOperateBase, IDetailIndexOperate
	{
		public bool Finished { get; set; }

		public NoRangeDetailIndexOperate(ExternalCommandData revit, DetailIndexParameter detailIndexPara) : base(revit, detailIndexPara)
		{
			this.Finished = false;
		}

		public Result Operate()
		{
			base.TempElementIds.Clear();
			Transaction transaction = new Transaction(this.m_Doc, "Create TempElement");
			DetailIndexJig detailIndexJig = new DetailIndexJig(this.m_UIDoc, base.IfUseCustomAngle, this.Angle);
			Result result;
			try
			{
				transaction.Start();
				XYZ xyz = detailIndexJig.PickStartPoint("请选择第一点");
				base.m_DetailIndexParameter.StartPoint = xyz;
				XYZ xyz2 = detailIndexJig.PickElbowPoint(xyz, "请选择转折点");
				base.m_DetailIndexParameter.ElbowPoint = xyz2;
				base.TempElementIds.Add(this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, Line.CreateBound(xyz, xyz2)).Id);
				XYZ xyz3 = detailIndexJig.PickEndPoint(xyz2, "请选择标注终点");
				base.m_DetailIndexParameter.EndPoint = xyz3;
				detailIndexJig.Dispose();
				base.m_DetailIndexParameter.IsForward = base.GetForward(xyz2, xyz3);
				base.m_DetailIndexParameter.BaseLineLength = xyz3.DistanceTo(xyz2) / (double)this.m_Doc.ActiveView.Scale;
				this.Finished = true;
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				transaction.RollBack();
				base.DeleteTempElement();
				detailIndexJig.Dispose();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public bool Create(DetailIndexParameter detailIndexPara)
		{
			base.UpdateDetailIndexParameter(detailIndexPara);
			if (!this.EditFamily())
			{
				base.DeleteTempElement();
				return false;
			}
			if (!base.CreateFamilySymbol())
			{
				base.DeleteTempElement();
				return false;
			}
			return base.DeleteTempElement();
		}

		public bool EditFamily()
		{
			this.m_TargetFamilyDocPath = this.m_SourceFamilyDocPath;
			return true;
		}
	}
}
